package com.zmn.plat.admin.controller.channel.promotion;

import java.util.List;
import java.util.Optional;

import com.zmn.plat.model.entity.channel.Channel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.zmn.plat.model.entity.channel.promotion.ChannelPromotion;
import com.zmn.plat.model.entity.channel.promotion.ChannelPromotionQuery;



/**
 * 渠道推广配置(当前只有今日头条使用)
 *
 * @author linfeng
 *
 * @since 2020/11/20 17:01
 */
@Controller
@RequestMapping("/channel/promotion/")
public class ChannelPromotionListController extends ChannelPromotionBaseController {

    /**
     * 列表
     * @author linfeng
     * @param query
     * @since 2020/11/23 16:41
     */
    @RequestMapping("list")
    @RequiresPermissions("/channel/promotion/list.action")
    public ModelAndView list(@ModelAttribute ChannelPromotionQuery query){

        List<ChannelPromotion> items = channelPromotionService.listPageByQuery(query);
        Channel channel = Optional.ofNullable(channelService.findByKey(query.getChannelId())).orElse(new Channel());
        Integer mergeSettle = getMergeSettle(query.getChannelId());
        channel.setMergeSettle(mergeSettle);
        ModelAndView mav = new ModelAndView("channel/promotion/list");
        mav.addObject("items", items);
        mav.addObject("vo", query);
        mav.addObject("obj", channel);
        mav.addObject("flag", 14);
        return mav;
    }


    /**
     * 详情
     * @author linfeng
     * @param relateId
     * @since 2020/11/23 16:41
     */
    @RequestMapping("detail")
    @RequiresPermissions("/channel/promotion/detail.action")
    public ModelAndView detail(@RequestParam(value = "channelId", defaultValue = "0") Integer channelId,
                               @RequestParam(value = "relateId", defaultValue = "0") Integer relateId){
        ChannelPromotion channelPromotion;
        if (relateId == 0) {
            channelPromotion = new ChannelPromotion();
            channelPromotion.setChannelId(channelId);
        } else {
            channelPromotion = channelPromotionService.findByKey(relateId);
        }

        Channel channel = Optional.ofNullable(channelService.findByKey(channelPromotion.getChannelId())).orElse(new Channel());

        ModelAndView mav = new ModelAndView("channel/promotion/detail");
        mav.addObject("obj", channelPromotion);
        mav.addObject("channel", channel);
        return mav;
    }

}
